<?php
namespace app\common\logic;

use think\facade\Db;

class Upload
{
    private function index($inputname = '')
    {
        $file = request()->file($inputname);
        $info = $file->getInfo();
        $newname = preg_replace('/\/tmp\/\w+/', '/tmp/'.$info['name'], $info['tmp_name']);
        rename($info['tmp_name'], $newname);
        $type = config('app.update_type');
        $file_temp = explode('.', $newname);
        $md5key = md5_file($newname);
        // 判断数据库文件是否已经存在
        $file = Db::name('files')->where('md5key', $md5key)->where('upload_type', $type)->find();
        if($file){
            switch($type){
                case 'cos':
                    $conf = config('cos.');
                    $cosClient = new \Qcloud\Cos\Client([
                        'region' => $conf['COS_REGION'],
                        'credentials'=> [
                            'appId' => $conf['COS_APPID'],
                            'secretId'    => $conf['COS_KEY'],
                            'secretKey' => $conf['COS_SECRET']
                        ]
                    ]);
                    $temp = $cosClient->getObjectUrl($conf['COS_BUCKET'], $file['file_path']);
                    if(input('param.k') == 'quill'){
                        $temp = explode('?', $temp)[0];
                    }
                    return json_return('10000', '上传成功', [
                        'k' => input('param.k'),
                        'url' => $temp,
                        'id' => $file['id']
                    ]);
                break;
                case 'local':
                    return json_return('10000', '上传成功', [
                        'k' => input('param.k'),
                        'url' => $file['file_path'],
                        'id' => $file['id']
                    ]);
                break;
            }
        }
        $savedata = [
            'upload_type' => $type,
            'md5key' => $md5key,
            'file_name' => $info['name'],
            'file_path' => ''
        ];
        switch($type){
            case 'cos':
                $conf = config('cos.');
                $cosClient = new \Qcloud\Cos\Client([
                    'region' => $conf['COS_REGION'],
                    'credentials'=> [
                        'appId' => $conf['COS_APPID'],
                        'secretId'    => $conf['COS_KEY'],
                        'secretKey' => $conf['COS_SECRET']
                    ]
                ]);
                try {
                    $cosfilename = date('Ymd').'/'.$md5key.'.'.$file_temp[count($file_temp)-1];
                    $result = $cosClient->putObject([
                        'Bucket' => $conf['COS_BUCKET'],
                        'Key' => $cosfilename,
                        'Body' => fopen($newname, 'rb')
                    ]);
                    $temp = $cosClient->getObjectUrl($conf['COS_BUCKET'], $cosfilename);
                    if(input('param.k') == 'quill'){
                        $temp = explode('?', $temp)[0];
                    }
                    //入数据库
                    $savedata['file_path'] = $cosfilename;
                    $id = Db::name('files')->insertGetId($savedata);
                    return json_return('10000', '上传成功', [
                        'k' => input('param.k'),
                        'url' => $temp,
                        'id' => $id
                    ]);
                } catch (\Exception $e) {
                    return json_return('10001', '', ['error' => $e]);
                }
                break;
            case 'local':
                $conf = config('upload.');
                $pre = 'uploads/'.date('Ymd');
                $path = \think\facade\App::getRootPath().'public/'.$pre;
                if(!file_exists($path)){
                    mkdir($path, 0644, true);
                }
                $filename = $md5key.'.'.$file_temp[count($file_temp)-1];
                $savename = $path.'/'.$filename;
                //入数据库
                $savedata['file_path'] = '/'.$pre.'/'.$filename;
                try{
                    rename($newname, $savename);
                }catch(\Exception $e){
                }
                $id = Db::name('files')->insertGetId($savedata);
                return json_return('10000', '上传成功', [
                    'k' => input('param.k'),
                    'url' => $savedata['file_path'],
                    'id' => $id
                ]);
                break;
        }
        
    }

    public function img()
    {
        $file = $this->index('img');
        return $file;
    }

    public function file()
    {
        $file = $this->index('file');
        return $file;
    }
}